package jafou.server;

import jafou.Model.Forum;
import jafou.Model.JaFouUser;
import jafou.Model.PMF;
import jafou.Model.Topic;

import java.util.List;

import javax.jdo.PersistenceManager;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class ForumHandler extends RemoteServiceServlet {
	private JafouEnv env = new JafouEnv();
	
	Forum getForum(String key) {
		Forum f = null;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			if(key != null){
				f = pm.getObjectById(Forum.class, Long.parseLong(key));
			}
		} finally {
			pm.close();
		}
		return f;
	}

	Forum[] getAllForums() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Forum[] forums = new Forum[0];
		try {
			String query = "select from " + Forum.class.getName();
			List<Forum> fList = (List<Forum>) pm.newQuery(query).execute();
			if (!fList.isEmpty()) {
				forums = new Forum[fList.size()];
				for (int i = 0; i < forums.length; i++) {
					forums[i] = fList.get(i);
				}
			}
		} finally {
			pm.close();
		}
		return forums;
	}

	private String getForumIdForTitle(String title) {
		String id = null;

		PersistenceManager pm = PMF.get().getPersistenceManager();
		Forum[] forums = new Forum[0];
		try {
			String query = "select from " + Forum.class.getName()
					+ " where title == \"" + title + "\"";
			List<Forum> fList = (List<Forum>) pm.newQuery(query).execute();
			if (!fList.isEmpty()) {
				id = "" + fList.get(0).getId();
			}
		} finally {
			pm.close();
		}
		return id;
	}

	private Topic[] getTopicsForForum(String forumId) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Topic[] topics = new Topic[0];
		try {
			String query = "select from " + Topic.class.getName()
					+ " where forumId == \"" + forumId + "\"";
			List<Topic> tList = (List<Topic>) pm.newQuery(query).execute();
			if (!tList.isEmpty()) {
				topics = new Topic[tList.size()];
				for (int i = 0; i < topics.length; i++) {
					topics[i] = tList.get(i);
				}
			}
		} finally {
			pm.close();
		}
		return topics;
	}
	
	public String[] getForums() {
		Forum[] forums = getAllForums();
		String[] forumNames = new String[forums.length];
		for (int i = 0; i < forumNames.length; i++) {
			forumNames[i] = forums[i].getTitle();
		}
		return forumNames;
	}

	public String[][] getTopics(String forum) {
		String[][] topicObjs = new String[0][0];
		
		String id = forum;
		if (id != null) {
			Topic[] topics = getTopicsForForum(id);
			topicObjs = new String[topics.length][5];
			for (int i=0; i<topics.length; i++) {
				topicObjs[i][0] = topics[i].getSubject();
				topicObjs[i][1] = env.getUserNameFromId(topics[i].getUserId());
				topicObjs[i][2] = topics[i].getCreatedOn()!=null?topics[i].getCreatedOn().toString():"";
				topicObjs[i][3] = "" + topics[i].getNComments();
				topicObjs[i][4] = "" + topics[i].getId();
			}
		}
		return topicObjs;
	}

	public void delForum(String forumId) {
		TopicsHandler th = new TopicsHandler();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		if(forumId == null || "".equals(forumId.trim())){ return; }
		try {
			Forum f = pm.getObjectById(Forum.class, Long.parseLong(forumId));
			th.delTopicsForForum(forumId);
			pm.deletePersistent(f);
		} finally {
			pm.close();
		}
	}

	public String addForum(String title, String tagline) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String id = "";
		try {
			Forum forum = new Forum(title);
			forum.setTagline(tagline);
			pm.makePersistent(forum);
			id = "" + forum.getId();
		} finally {
			pm.close();
		}
		return id;
	}

}
